<?php

namespace app\admin\controller;

use think\Validate;
use think\Db;
use app\models\{Goods as Good};

class Goods extends Base
{


    public function goods()
    {
        if ($this->request->isAjax()) {
            $model = new Good();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['del', '=', 1];
            if (!empty($post['title'])) {
                $where[] = ['id|title', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['type'])) {
                $where[] = ['type', '=', $post['type']];
            }
            if (!empty($post['groups'])) {
                $where[] = ['groups', '=', $post['groups']];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            foreach ($list as $row) {
                $row['type_name'] = config()["config"]['type'][$row['groups']] ?? '无';
            }
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            $grouplist = config()["config"]['type'];
            return view(null, ['grouplist' => $grouplist]);
        }
    }

    public function goods_add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'type' => 'require',
                'groups' => 'require',
            ], [
                'title.require' => '账号不能为空',
                'type.require' => '职位类型不能为空',
                'groups.require' => '组别不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['title'] = trim($data['title']);
            //查找账号
            $have = (new Good())->where('groups', $data['groups'])->where('del', 1)->where('title', $data['title'])->find();
            if (!empty($have) && $have['id'] != $data['id']) {
                return json(['code' => 1, 'msg' => '该分组对应物料已存在']);
            }

            if (empty($data['id'])) {
                $res = (new Good())->allowfield(true)->save($data);
            } else {
                $res = (new Good())->allowfield(true)->save($data, ['id' => $data['id']]);
            }
            if ($res) {
                return json(['code' => 200, 'status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['code' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Good())->where('id', $id)->find();

            $grouplist = config()["config"]['type'];
            return view('', [
                'row' => $row,
                'grouplist' => $grouplist
            ]);
        }
    }

    public function goods_add2()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'type' => 'require',
                'total' => 'require|gt:0|float',
            ], [
                'title.require' => '账号不能为空',
                'type.require' => '职位类型不能为空',
                'total.require' => '赠品价格不能为空',
                'total.gt' => '赠品价格必须大于0',
                'total.float' => '赠品价格必须是数字',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['title'] = trim($data['title']);
            //查找账号
            $have = (new Good())->where('type', $data['type'])->where('del', 1)->where('title', $data['title'])->find();
            if (!empty($have) && $have['id'] != $data['id']) {
                return json(['code' => 1, 'msg' => '该赠品信息已存在']);
            }
            if (empty($data['id'])) {
                $res = (new Good())->allowfield(true)->save($data);
            } else {
                $res = (new Good())->allowfield(true)->save($data, ['id' => $data['id']]);
            }
            if ($res) {
                return json(['code' => 200, 'status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['code' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Good())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }
}
